package com.hc.controller;



import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.hc.Operation;
import com.hc.bean.SysUserBean;
import com.hc.bean.SysUserRoleBean;
import com.hc.service.SysUserService;
import com.hc.util.JsonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



//如果方法返回的值都要转换为Json时，可以用 @ResetController代替@Controller和@ResonseBody注解
//@Controller
@RestController
@RequestMapping("/sysUserController")
@CrossOrigin
public class SysUserController  {

    Logger logger = LoggerFactory.getLogger(SysUserController.class.getName());

    @Autowired
    SysUserService sysUserService;

    @GetMapping("/queryData")
    @Operation("查询用户")
   @SaCheckPermission("sys:user:query")
    public JsonResponse queryData(SysUserBean sysUserBean,int pageNum,int pageSize){

        JsonResponse jsonResponse = new JsonResponse();
        try {

            PageInfo<SysUserBean> pageInfo = sysUserService.queryDataByPage(sysUserBean, pageNum, pageSize);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
            jsonResponse.setData(pageInfo);

        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;
    }





    //当方法参数为一个javaBean类型时，如果前端发来的请求参数格式为  username=admin&key=value格式时，可自动将请求参数放到javaBaen匹配的属性中
    //当方法参数为一个javaBean类型时，如果前端发来的请求参数格式为  {属性名:属性值,属性名:属性值时}
    //  这种是json格式，需要在方法参数前添加一个@ReqeustBody注解,spring会将json格式的数据转换为方法形参对应的javaBean类型



    //客户端传递的数据为:{ "user_id": null, "username": "123", "confirmPassword": "1", "password": "1", "email": "12196514@qq.com", "mobile": "11111", "status": 0, "shop_id": 1, "selectedRoleId": [ 1, 3 ] }
    @PostMapping("/saveUser")
    @Operation("新增用户")
    @SaCheckPermission("sys:user:insert")
    public JsonResponse saveUser(@RequestBody SysUserBean sysUserBean){
      //  StpUtil.checkPermission("sys:user:insert");
        JsonResponse jsonResponse = new JsonResponse();
        try {
             sysUserService.insertData(sysUserBean);
             jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");



        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;
    }



    @GetMapping("/userLogin")
    @Operation("用户登录")

    public JsonResponse userLogin(String userName,String password){

        JsonResponse jsonResponse = new JsonResponse();
        try {
            /**
             *1. 用户提交 name + password 参数，调用登录接口。(验证帐号密码)
             *2. 登录成功，返回这个用户的 Token 会话凭证。
             *      （satoken会根据userid生成一个 32位长度的字符串 04ccd44d-52a9-41ff-b6a7-049eff7851fa）
             *      以响应报文的形式在响应头中增加  Set-Cookie 值为为  satoken=04ccd44d-52a9-41ff-b6a7-049eff7851fa; Max-Age=2592000; Expires=Wed, 3 Sep 2025 16:30:38 +0800; Path=/
             *3.用户后续的每次请求，都携带上这个 Token。在请求头中携带
             *4.服务器根据 Token 判断此会话是否登录成功。
             */
            SysUserBean sysUserBean =      sysUserService.userLogin(userName,password);
          //  StpUtil.setLoginId(sysUserBean.getUser_id());//把当前登录用户的ID存储起来
            StpUtil.login(sysUserBean.getUser_id());
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            logger.debug("tokenName:"+tokenInfo.getTokenName()+" tokenValue:"+tokenInfo.getTokenValue());

            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");
            jsonResponse.setData(sysUserBean);
            jsonResponse.setSaTokenInfo( tokenInfo);

        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;

    }




    @DeleteMapping("/deleteUser/{id}") ///deleteUser?user_id=12
    //@PathVariable("id")     表示将请求路径中的{id}占位符对应的值取出来赋方法形参user_id
    @SaCheckPermission("sys:user:delete")
    public JsonResponse deleteUser(@PathVariable("id") Integer user_id){
      //  StpUtil.checkPermission("sys:user:delete");
        JsonResponse jsonResponse = new JsonResponse();
        try {
            sysUserService.deleteDataById(user_id);
            jsonResponse.setCode(0);
            jsonResponse.setMsg("OK");

        }catch (Exception e){
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg(e.getMessage());

        }

        return  jsonResponse;

    }





}
